const marketService = require('../service/MarketService');
const LambdaProxy = require('../utils/LambdaProxy');
const DateUtil = require('../utils/DateUtil');
const MarketPriceDto = require('../dto/MarketPriceDto');
const KLineDto = require('../dto/KLineDto');

// ---------- lambda entry ----------

/**
 * info
 * @param {Object} event
 * @param {Object} context
 * @return {Object}
 */
async function info(event, context) {
  console.info('entry market info', event);

  const { coinPair, more } = event.body;
  const [baseCoinType, counterCoinType] = coinPair.split('/');

  let info = await marketService.getMarketInfo(baseCoinType, counterCoinType, more);

  return info;
}

/**
 * marketPriceBetween
 * @param {event} event
 * @param {String} event.body.coinPair
 * @param {Integer} event.body.from
 * @param {Integer} event.body.to
 * @param {context} context
 * @return {Object}
 */
async function marketPriceBetween(event, context) {
  console.info('marketPriceBetween', event);

  const { coinPair, from, to } = event.body;
  const [baseCoinType, counterCoinType] = coinPair.split('/');

  let marketPrice = await marketService.getMarketPriceBetween(
    baseCoinType,
    counterCoinType,
    new Date(Number(from)),
    new Date(Number(to))
  );

  console.info('got marketPriceBetween:', marketPrice);

  return marketPrice;
}

/**
 * klineBetween
 * @param {event} event
 * @param {String} event.body.coinPair
 * @param {Integer} event.body.from
 * @param {Integer} event.body.to
 * @param {context} context
 * @return {Object}
 */
async function klineBetween(event, context) {
  console.info('klineBetween', event);

  const { coinPair, from, to } = event.body;
  const [baseCoinType, counterCoinType] = coinPair.split('/');

  let kline = await marketService.getKLineBetween(
    baseCoinType,
    counterCoinType,
    new Date(Number(from)),
    new Date(Number(to))
  );

  console.info('got klineBetween:', kline);

  return kline;
}

/**
 * listPrice
 * @param {event} event
 * @param {String} event.body.coinPair
 * @param {Integer} event.body.minutes
 * @param {Integer} event.body.from
 * @param {Integer} event.body.to
 * @param {context} context
 * @return {MarketPriceDto}
 */
async function listPrice(event, context) {
  console.info('listPrice', event);
  const { coinPair, minutes, from, to } = event.body;

  const result = await LambdaProxy.listPrice({ coinPair, minutes, from, to });

  const range = DateUtil.checkCurrentRange(Number(to), Number(minutes));

  if (range) {
    const [baseCoinType, counterCoinType] = coinPair.split('/');

    const marketPrice = await marketService.getMarketPriceBetween(
      baseCoinType,
      counterCoinType,
      new Date(range[0]),
      new Date(range[1])
    );
    marketPrice.TimePoint = range[0];

    result.Items.push(marketPrice);
  }

  return new MarketPriceDto(result.Items);
}

/**
 * listKLine
 * @param {event} event
 * @param {String} event.body.coinPair
 * @param {Integer} event.body.minutes
 * @param {Integer} event.body.from
 * @param {Integer} event.body.to
 * @param {context} context
 * @return {KLineDto}
 */
async function listKLine(event, context) {
  console.info('listKLine', event);
  const { coinPair, minutes, from, to } = event.body;

  const result = await LambdaProxy.listKLine(coinPair, minutes, from, to);

  const range = DateUtil.checkCurrentRange(Number(to), Number(minutes));

  if (range) {
    const [baseCoinType, counterCoinType] = coinPair.split('/');

    const kline = await marketService.getKLineBetween(
      baseCoinType,
      counterCoinType,
      new Date(range[0]),
      new Date(range[1])
    );

    result.Items.push(kline);
  }

  return new KLineDto(result.Items);
}

module.exports = {
  info,
  marketPriceBetween,
  klineBetween,
  listPrice,
  listKLine,
};

// ---------- internal functions ----------
